package com.zfei.gfb.service;

import cn.hutool.core.date.DateUtil;
import com.zfei.gfb.api.StatsService;
import com.zfei.gfb.api.TalkService;
import com.zfei.gfb.api.domain.StatsTag;
import com.zfei.gfb.api.domain.StatsValue;
import com.zfei.gfb.api.domain.TalkEvent;
import com.zfei.gfb.bean.Const;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.dao.GfbGreenPlotApplyMapper;
import com.zfei.gfb.dao.GfbGreenPlotMapper;
import com.zfei.gfb.device.AppContext;
import com.zfei.gfb.model.GfbGreenPlot;
import com.zfei.gfb.model.GfbGreenPlotApply;
import com.zfei.gfb.request.h5.GreenPlotClaimRequest;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Now;
import com.zfei.gfb.util.TalkUtil;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author duyujie
 * @date 2021/9/2
 **/
@Component
public class GreenPlotServiceImpl {

    @Resource
    private GfbGreenPlotMapper gfbGreenPlotMapper;

    @Resource
    private GfbGreenPlotApplyMapper gfbGreenPlotApplyMapper;

    @Resource
    private HttpSession httpSession;

    @Resource
    private TalkService talkService;

    @Resource
    private StatsService statsService;

    @Transactional(rollbackFor = Exception.class)
    public Object collect(GreenPlotClaimRequest request) {
        AppContext context = AppContext.getInstance();
        String identifierId = context.getIdentifierId();
        long now = Now.millis();

        Integer greenPlotId = request.getGreenPlotId();
        if (greenPlotId == null) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        GfbGreenPlot greenPlot = gfbGreenPlotMapper.selectByPrimaryKey(greenPlotId);
        if (greenPlot == null) {
            throw new StatusException(Status.NOT_EXIST);
        }

        TalkEvent claim = this.talkService.getClaim(identifierId, greenPlotId.toString());
        if (claim != null && claim.getDeleteTime() == 0) {
            throw new StatusException(Status.DUPLICATE);
        }

        if (claim == null) {
            this.talkService.createEvent(TalkUtil.buildClaim(greenPlot, identifierId, now,request));
        } else {
            this.talkService.recoveryEvent(claim.getEventId());
        }

        StatsValue postStats = new StatsValue(greenPlot.getId().toString(), StatsTag.GREEN_CLAIM_COUNT, 1);
        statsService.inc(postStats);


        GfbGreenPlotApply record = Beans.copy(request, GfbGreenPlotApply.class);
        record.setUserId(AppContext.getInstance().getUid());
        record.setStatus(Const.STATUS_ON);
        record.setPlotId(request.getGreenPlotId());
        record.setCreateTime(Now.millis());
        record.setUpdateTime(Now.millis());
        record.setCntime(DateUtil.now());
        gfbGreenPlotApplyMapper.insertSelective(record);

        return null;
    }
}
